import pytest
import requests

class TestCase():
    #登录token提取变量
    token = None
    def setup_method(self):
        print("测试前执行操作")
    def teardown_method(self):
        print("结束后执行")

    #登录
    def test_login(self):
        url="http://10.59.9.12:7001/api/public/login"
        head={"Content-Type": "application/json"}
        data={"username":"13811033962","password":"123456"}
        res = requests.post(url=url,headers=head,json=data)
        print(res.json())
        #提取变量
        TestCase.token ="Bearer "+res.json()["data"]["token"]
        #断言
        assert "ok" in res.text

    #确认订单接口测试用例5条

    #1、确认订单接口所有参数信息正确，确认订单成功
    def test_confirm01(self):
        url ="http://10.59.9.12:7001/api/order/confirm?id=69&type=buy&sku_id=&num=1&shipping_type=1"
        head ={"Auth-Token":TestCase.token}
        res = requests.get(url=url,headers=head)
        print(res.json())
        assert res.json()["status"] == 1
        assert res.json()["info"] == "ok"

    # 2、确认订单接口id参数值为空，确认订单失败
    def test_confirm02(self):
        url ="http://10.59.9.12:7001/api/order/confirm?id=""&type=buy&sku_id=&num=1&shipping_type=1"
        head ={"Auth-Token":TestCase.token}
        res = requests.get(url=url,headers=head)
        print(res.json())

        assert res.json()["info"] == '请选择需要购买的商品'

    # 3、确认订单接口用户未登录，确认订单失败
    def test_confirm03(self):
        url ="http://10.59.9.12:7001/api/order/confirm?id=69&type=buy&sku_id=&num=1&shipping_type=1"
        res = requests.get(url=url)
        print(res.json())

        assert '未登录' in res.text
        assert res.status_code==200

    #4、确认订单接口商品数量为空，确认订单失败
    def test_confirm04(self):
        url ="http://10.59.9.12:7001/api/order/confirm?id=69&type=buy&sku_id=&num=**//---55558841、、、1&shipping_type=1"
        head ={"Auth-Token":TestCase.token}
        res = requests.get(url=url,headers=head)
        print(res.json())
        assert res.json()["data"]["item"][0]["goods_id"] == 69
    #5、确认订单接口缺少sku_id参数，确认订单失败
    def test_confirm05(self):
        url ="http://10.59.9.12:7001/api/order/confirm?id=69&type=buy&num=&shipping_type=1"
        head ={"Auth-Token":TestCase.token}
        res = requests.get(url=url,headers=head)
        print(res.json())

        assert "北京" in res.text